package com.asiainfo.iam.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Service
public class DMDatabaseConnService {


    private static final Logger log  = LoggerFactory.getLogger(DMDatabaseConnService.class);

    public Connection createConn(String host, String port, String user, String password){
        String url = "jdbc:dm://" + host + ":" + port;
        try {
            // 加载达梦数据库 JDBC 驱动
            Class.forName("dm.jdbc.driver.DmDriver");

            // 建立数据库连接
            return DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException var7) {
            // 记录并处理类未找到异常（通常是驱动未引入）
            log.error("发生了 ClassNotFoundException 异常: {}", var7.getMessage());
            throw new RuntimeException("无法找到达梦数据库驱动类：dm.jdbc.driver.DmDriver", var7);
        } catch (
                SQLException var8) {
            // 记录并处理 SQL 异常（如连接失败、认证错误等）
            log.error("发生了 SQLException 异常: {}", var8.getMessage());
            throw new RuntimeException("数据库连接失败：" + var8.getMessage(), var8);
        }
    }



}
